package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.ImagePiece;
import flc.ast.databinding.ActivityPicCutBinding;
import flc.ast.utils.Content;
import flc.ast.utils.ImageSplitter;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class PicCutActivity extends BaseAc<ActivityPicCutBinding> {

    public static String imgPath = "";
    private Bitmap mImgBitmap;
    private int type;
    private List<ImagePiece> pictureSplitList = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_cut;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicCutBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPicCutSave.setOnClickListener(this);
        mDataBinding.ivFourBoxGrid.setOnClickListener(this);
        mDataBinding.ivNineBoxGrid.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivFourBoxGrid://四宫格
                clearSelection();
                mDataBinding.ivFourBoxGrid.setImageResource(R.drawable.an_sgg);
                type = 0;
                break;
            case R.id.ivNineBoxGrid://九宫格
                clearSelection();
                mDataBinding.ivNineBoxGrid.setImageResource(R.drawable.an_jgg);
                type = 1;
                break;
            case R.id.ivPicCutSave://保存
                if (type == 0) {
                    saveImg(2);
                } else {
                    saveImg(3);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        //设置图片
        setImg();
    }

    //保存图片
    private void saveImg(int num) {
        showDialog(getString(R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.ivPicCutImg.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap, (int) cropRect.left, (int) cropRect.top, (int) cropRect.width(), (int) cropRect.height());
                emitter.onNext(resultBit);
            }

            @Override
            public void accept(Bitmap bitmap) {
                if (bitmap != null) {
                    pictureSplitList = ImageSplitter.split(bitmap, num, num);
                    for (ImagePiece imagePiece : pictureSplitList) {
                        //保存到历史记录
                        String resultFile = FileUtil.generateFilePath(Content.myRecord, ".png");
                        ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                        //保存到相册
                        ImageUtils.save2Album(imagePiece.bitmap, Bitmap.CompressFormat.PNG);
                    }
                    ToastUtils.showShort(R.string.save_suc);
                    startActivity(HomeActivity.class);
                }
                dismissDialog();
            }
        });
    }

    //设置图片
    private void setImg() {
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.ivPicCutImg.setImageBitmap(mImgBitmap);
        mDataBinding.ivPicCutImg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.ivPicCutImg.setScaleEnabled(false);// 禁用缩放
        mDataBinding.ivPicCutImg.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.ivPicCutImg.getBitmapRect();
                mDataBinding.cropImgView.setCropRect(bitmapRect);
            }
        });
    }

    private void clearSelection() {
        mDataBinding.ivFourBoxGrid.setImageResource(R.drawable.an_sgg1);
        mDataBinding.ivNineBoxGrid.setImageResource(R.drawable.an_jgg1);
    }

}